home *** CD-ROM | disk | FTP | other *** search
/ PC Elektro 3 / PC-Elektro-3-cd1.bin / KBan 2.0 / KBANSRC.LZH / SRC / PROG / KBANDOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-24  |  7.3 KB  |  289 lines

  1. // the implementation of class CKBANDoc
  2. // Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include "bmpout.h"
  7. #include "dlgbmp.h"
  8. #include "draw.h"
  9. #include "drawbmp.h"
  10. #include "memdc.h"
  11. #include "netlist/netlist.h"
  12. #include "resource.h"
  13.  
  14. #include "kbandoc.h"
  15.  
  16. // member variable(s)
  17. IMPLEMENT_DYNCREATE(CKBANDoc, CDocument)
  18.  
  19. BEGIN_MESSAGE_MAP(CKBANDoc, CDocument)
  20.   ON_COMMAND(ID_FILE_SAVE_BMP, OnSaveAsBitmap)
  21.   ON_COMMAND(ID_FILE_SAVE_CMP, OnSaveAsComponent)
  22.   ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
  23.   ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
  24.   ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_BMP, OnUpdateFileSaveBmp)
  25.   ON_COMMAND(ID_FILE_SAVE_GBR, OnSaveAsGerber)
  26.   ON_COMMAND(ID_FILE_OUTPUT_NETLIST, OnOutputNetlist)
  27. END_MESSAGE_MAP()
  28.  
  29. // constructor(s)
  30. CKBANDoc::CKBANDoc()
  31.   : m_undo_history(m_info)
  32. {
  33.   m_info.AssociateDocument(*this);
  34. }
  35.  
  36. // destructor
  37. CKBANDoc::~CKBANDoc()
  38. {
  39. }
  40.  
  41. // member functions
  42. bool CKBANDoc::Undo()
  43. {
  44.   bool result;
  45.   if(result = m_undo_history.Undo()) {
  46.     m_info = m_undo_history.GetCurrent();
  47.   }
  48.   return result;
  49. }
  50.  
  51. bool CKBANDoc::Redo()
  52. {
  53.   bool result;
  54.   if(result = m_undo_history.Redo()) {
  55.     m_info = m_undo_history.GetCurrent();
  56.   }
  57.   return result;
  58. }
  59.  
  60. void CKBANDoc::ClearHistory()
  61. {
  62.   m_undo_history.ClearHistory();
  63. }
  64.  
  65. void CKBANDoc::SetNewState(const char* name)
  66. {
  67.   m_undo_history.SetNewState(m_info, name);
  68. }
  69.  
  70. const const char* CKBANDoc::GetUndoName() const
  71. {
  72.   return m_undo_history.GetUndoName().c_str();
  73. }
  74.  
  75. const const char* CKBANDoc::GetRedoName() const
  76. {
  77.   return m_undo_history.GetRedoName().c_str();
  78. }
  79.  
  80. BOOL CKBANDoc::Load(LPCTSTR lpszPathName)
  81. {
  82.   FILE_NEW fp(lpszPathName, "r");
  83.   kban_info().kban_data().load(fp);
  84.   kban_info().kban_data().collect_aperture(
  85.     kban_info().apt_pin_table(),
  86.     kban_info().apt_line_table()
  87.   );
  88.   return TRUE;
  89. }
  90.  
  91. BOOL CKBANDoc::Save(LPCTSTR lpszPathName)
  92. {
  93.   FILE_NEW fp(lpszPathName, "w");
  94.   kban_info().kban_data().save(fp);
  95.   return TRUE;
  96. }
  97.  
  98. BOOL CKBANDoc::OnNewDocument()
  99. {
  100.   if(!CDocument::OnNewDocument()) {
  101.     return FALSE;
  102.   }
  103.   return TRUE;
  104. }
  105.  
  106. BOOL CKBANDoc::OnOpenDocument(LPCTSTR lpszPathName)
  107. {
  108.   if(!CDocument::OnOpenDocument(lpszPathName)) {
  109.     return FALSE;
  110.   }
  111.   Load(lpszPathName);
  112.   return TRUE;
  113. }
  114.  
  115. BOOL CKBANDoc::OnSaveDocument(LPCTSTR lpszPathName)
  116. {
  117.   if(!CDocument::OnSaveDocument(lpszPathName)) {
  118.     return FALSE;
  119.   }
  120.   Save(lpszPathName);
  121.   return TRUE;
  122. }
  123.  
  124. void CKBANDoc::DeleteContents()
  125. {
  126.   kban_info().kban_data().clear();
  127. }
  128.  
  129. void CKBANDoc::Serialize(CArchive& ar)
  130. {
  131. }
  132.  
  133. void CKBANDoc::SaveBitmap(KBAN_INFO& info, const SAVEASBMP_INFO& sbinfo)
  134. {
  135.   FILE_NEW fp(sbinfo.m_fname, "wb");
  136.   if(!fp.is_ok()) {
  137.     AfxGetMainWnd()->MessageBox("Failed to open the specified file", "Save As Bitmap");
  138.   } else {
  139.     KBAN_DATA kban_data = info.kban_data();
  140.     if(sbinfo.m_limit_drill.get()) {
  141.       kban_data.limit_drill_size(sbinfo.m_limit_drill_size);
  142.     }
  143.     XY ac_min = kban_data.get_min();
  144.     XY ac_max = kban_data.get_max();
  145.  
  146.     // adds margin
  147.     ac_min -= DIS_DINCH;
  148.     ac_max += DIS_DINCH;
  149.  
  150.     DRAW_BMP_INFO draw_info(ac_min, ac_max, DPI_DESIGN, sbinfo.m_dpi);
  151.     XY pc_win_size = draw_info.pc_win_size();
  152.  
  153.     // sets up a memory DC for a bitmap
  154.     CFrameWnd* pFrameWnd = (CFrameWnd*)AfxGetMainWnd();
  155.     CClientDC clientDC(pFrameWnd->GetActiveView());
  156.     CMemoryDC memDC(&clientDC, pc_win_size.x(), pc_win_size.y(), 1);
  157.     memDC.FillSolidRect(0, 0, pc_win_size.x(), pc_win_size.y(), RGB(255, 255, 255));
  158.  
  159.     // draws the data
  160.     KBAN_DRAW draw(&memDC, draw_info, sbinfo.m_fill, sbinfo.m_hole, sbinfo.m_layer);
  161.     draw.draw_kban_data(kban_data, info.active_layer().get());
  162.  
  163.     OutputBMPFile(fp, memDC.GetBitmap(), clientDC);
  164.   }
  165. }
  166.  
  167. void CKBANDoc::OnSaveAsBitmap()
  168. {
  169.     CSaveAsBitmapDialog dlg(m_sbinfo, AfxGetMainWnd());
  170.     if(dlg.DoModal() == IDOK) {
  171.         m_sbinfo = dlg.GetResult();
  172.         rec << "fname = " << m_sbinfo.m_fname << "\n";
  173.         SaveBitmap(kban_info(), m_sbinfo);
  174.     }
  175. }
  176.  
  177. void CKBANDoc::OnSaveAsComponent()
  178. {
  179.   CString fname;
  180.   CFileDialog dlg(FALSE, "cmp", fname,
  181.     OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
  182.     "KBAN Component Files (*.cmp)|*.cmp|All Files (*.*)|*.*||",
  183.     AfxGetMainWnd()
  184.   );
  185.   if(dlg.DoModal() == IDOK) {
  186.     fname = dlg.GetPathName();
  187.     FILE_NEW fp(fname, "w");
  188.     kban_info().kban_data().save(fp);
  189.   }
  190. }
  191.  
  192. class WIN_NETLIST_ERROR : public NETLIST_ERROR {
  193. public:
  194.   WIN_NETLIST_ERROR() {};
  195.   virtual void not_distinct(const char* comp_name);
  196. };
  197.  
  198. void WIN_NETLIST_ERROR::not_distinct(const char* comp_name)
  199. {
  200.     char mes[300];
  201.     sprintf(mes, "Warning: The designator \"%s\" is used more than once.", comp_name);
  202.     AfxGetMainWnd()->MessageBox(mes, "Output Netlist", MB_ICONWARNING);
  203. }
  204.  
  205. void CKBANDoc::OnOutputNetlist()
  206. {
  207.   CString fname;
  208.   CFileDialog dlg(FALSE, "net", fname,
  209.     OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
  210.     "KBAN Netlist Files (*.net)|*.net|All Files (*.*)|*.*||",
  211.     AfxGetMainWnd()
  212.   );
  213.   if(dlg.DoModal() == IDOK) {
  214.     WIN_NETLIST_ERROR ne;
  215.     fname = dlg.GetPathName();
  216.     output_netlist(ne, fname, kban_info().kban_data());
  217.   }
  218. }
  219.  
  220. void CKBANDoc::OnUpdateFileSaveBmp(CCmdUI* pCmdUI)
  221. {
  222.   pCmdUI->Enable(!kban_info().kban_data().empty());
  223. }
  224.  
  225. void CKBANDoc::OnSaveAsGerber()
  226. {
  227.   AfxGetMainWnd()->MessageBox("Hello", "OnSaveGerber");
  228.   KBAN_INFO& info = kban_info();
  229.   APT_TABLE apt_regular_table;
  230.   APT_TABLE apt_drill_table;
  231.   APT_TABLE apt_pin_table;
  232.   APT_TABLE apt_line_table;
  233.   info.kban_data().collect_aperture(apt_pin_table, apt_line_table);
  234.   uint i;
  235.   REC << "apt_pin_table\n";
  236.   FILE_NEW fp("gerber.txt", "w");
  237.   for(i = 0; i < apt_pin_table.size(); i++) {
  238.     const APERTURE& current = apt_pin_table[i];
  239.     fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
  240.       current.type(),
  241.       current.width(),
  242.       current.height(),
  243.       current.drill()
  244.     );
  245.     if(!apt_regular_table.is_included(current)) {
  246.       apt_regular_table.push_back(current);
  247.     }
  248.     APERTURE apt(APERTURE::APT_ROUND, 0, 0, current.drill());
  249.     if(!apt_drill_table.is_included(apt)) {
  250.       apt_drill_table.push_back(apt);
  251.     }
  252.   }
  253.   REC << "apt_line_table\n";
  254.   for(i = 0; i < apt_line_table.size(); i++) {
  255.     const APERTURE& current = apt_line_table[i];
  256.     fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
  257.       current.type(),
  258.       current.width(),
  259.       current.height(),
  260.       current.drill()
  261.     );
  262.     if(!apt_regular_table.is_included(current)) {
  263.       apt_regular_table.push_back(current);
  264.     }
  265.   }
  266.   fp.printf("---\n");
  267.   apt_regular_table.sort();
  268.   for(i = 0; i < apt_regular_table.size(); i++) {
  269.     const APERTURE& current = apt_regular_table[i];
  270.     fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
  271.       current.type(),
  272.       current.width(),
  273.       current.height(),
  274.       current.drill()
  275.     );
  276.   }
  277.   fp.printf("---\n");
  278.   apt_drill_table.sort();
  279.   for(i = 0; i < apt_drill_table.size(); i++) {
  280.     const APERTURE& current = apt_drill_table[i];
  281.     fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
  282.       current.type(),
  283.       current.width(),
  284.       current.height(),
  285.       current.drill()
  286.     );
  287.   }
  288. }
  289.